function [ ] = detectForegroundAFD( path, filename, frames, initframes )
    imgname_last = sprintf('%s%s_%04d.jpeg',path,filename,0);
    img_last = imread(imgname_last);

    for idx=initframes:initframes+frames-1

        imgname_current = sprintf('%s%s_%04d.jpeg',path,filename,idx);
        img_current = imread(imgname_current);


        diff = abs(img_current - img_last);

        diff_bw = im2bw(diff,0.0240);
        diff_bw = medfilt2(diff_bw,[5 5]);
        
        se = strel('disk',5);
        diff_bw = imclose(diff_bw,se);
        imwrite(diff_bw,
        sprintf('%sSeg_%s_%04d.png',path,filename,idx),'BitDepth',1);
      
        imshow(diff_bw);
        pause(0.01);

        img_last = img_current;
    end

end

